package com.lhz.services;

import com.lhz.dao.DingDanDao;
import com.lhz.dao.mybatis.ReportMybatis;
import com.lhz.vo.GoodsVo;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional//统计报表
public class ReportService {
	
    @Autowired
    private DingDanDao dingDanDao;
    
    @Autowired
    private ReportMybatis mybatis;
    
    
    Date date=new Date();
	int year=date.getYear()+1900;
	int month=date.getMonth()+1;
	int day=date.getDate();
	String thisMonth=month>=10?month+"":"0"+month;
    
    
    
    /**
     * 商品上周销量报表
     * @return
     */
	public Map<String, Object> prevWeekSellsNumReport(){
		Map<String, Object> map=new HashMap<>();
		String startDay=getLastTimeInterval().split(",")[0];
		String endDay=getLastTimeInterval().split(",")[1];
		List<GoodsVo> info=mybatis.searchBySellsNumReport(startDay, endDay);
		if(info!=null) {
			map.put("report", info);
		}else {
			throw new RuntimeException("没有找到相关信息.....");
		}
		return map;
	}
    
	
	/**
	 * 商品上月销量报表（发散饼状图）
	 * @return
	 */
	public Map<String, Object> prevMonthSellsNumReport(){
		Map<String, Object> map=new HashMap<>();
		String lastMonth=lastMonth(1);
		String years=year+"-"+thisMonth;
		List<GoodsVo> info=mybatis.searchByMonthSellsNumReport(lastMonth, years);
		if(info!=null) {
			map.put("report", info);
		}else {
			throw new RuntimeException("没有找到相关信息.....");
		}
		return map;
	}
		
	/**
	 * 上三月营业额日报表（折线图）
	 * @return
	 */
	public Map<String,Object> monthYurnoverReport(){
		Map<String,Object> map=new HashMap<>();
		for(int j=1;j<=3;j++){	
			//List<GoodsVo> list=new ArrayList<GoodsVo>();
			String lastMonth=lastMonth(j);
			for(int i=1;i<31;i++){
				String lastMonths=lastMonth+"-"+(i+1);
				List<GoodsVo> info=mybatis.searchBymonthYurnoverReport(lastMonth, lastMonths);
				map.put(lastMonth, info);
			}
		}
		return map;
    
	}
    
	/**
	 * 前三年营业额分月报表(柱状图)
	 * 
	 */
	public Map<Integer,Object> yearYurnoverReport(){
		Map<Integer,Object> map=new HashMap<>();
		for(int j=(year-1);j>=year-3;j--){
			//List<GoodsVo> list=new ArrayList<GoodsVo>();
			for(int i=1;i<=12;i++){
					String report=j+"-";
				if(i==12){
					report+="12' and date(riqi)<'"+(j+1)+"-01'";
				}else if(i<10){
					report+="0"+i+"' and date(riqi)<'"+j+"-0"+(i+1)+"'";
				}else{
					report+=i+"' and date(riqi)<'"+j+"-"+(i+1)+"'";
				}
				List<GoodsVo> info = mybatis.searchByearYurnoverReport(report);
				map.put(j, info);
			}
			
		}
		return map;
	}
	
	
	/**
	 * 五年内总营业额年报表(饼状图)
	 * @return
	 */
	public Map<Integer,Object> yearsTurnoverReport(){
		Map<Integer,Object> map=new HashMap<Integer,Object>();
		   Calendar c = Calendar.getInstance();
		     c.add(Calendar.YEAR, -4);
		     Date resultDate = c.getTime(); // 结果  
		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		     String date2=sdf.format(resultDate);
		     String satrYear = date2.substring(0, 4);//通过获取系统当前时间日历类计算截取出前五年的年份
		     /*int satrYear = Integer.parseInt(substring);*/
		for(int i=year;i>=year-4;i--){
			int endYear=(i+1);
			List<GoodsVo> info = mybatis.searchByYearsTurnoverReport(satrYear, endYear);
			map.put(i, info);
			 
		}
		return map;
	}
	
	
	
	
    
	/**
	 * 获取上n个月时间
	 * 
	 */
	private String lastMonth(int i){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -i);
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
		String time = format.format(c.getTime());
		return time;
	}
	
	/**
	 *	获取上个月有多少天 
	 * 
	 */
	private int lastMonthNum(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		//日期减一,取得上月最后一天时间对象
		cal.add(Calendar.DAY_OF_MONTH, -1);
		int a=cal.get(Calendar.DAY_OF_MONTH);
		return a;
	}
	
	/** 
	* 根据当前日期获得上周的日期区间（上周周一和周日日期） 
	*  
	*/  
	private String getLastTimeInterval() {  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	     Calendar calendar1 = Calendar.getInstance();  
	     Calendar calendar2 = Calendar.getInstance();  
	     int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;  
	     int offset1 = 1 - dayOfWeek;  
	     int offset2 = 7 - dayOfWeek;  
	     calendar1.add(Calendar.DATE, offset1 - 7);  
	     calendar2.add(Calendar.DATE, offset2 - 7);  
	     // System.out.println(sdf.format(calendar1.getTime()));// last Monday  
	     String lastBeginDate = sdf.format(calendar1.getTime());  
	     // System.out.println(sdf.format(calendar2.getTime()));// last Sunday  
	     String lastEndDate = sdf.format(calendar2.getTime());  
	     return lastBeginDate + "," + lastEndDate;  
	} 
    
    
    
    
    
    
    
    


}
